package com.dd.cloud.common.config.map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dd.cloud.common.utils.HttpRequestUtils;
import com.dd.cloud.common.vo.LngLatInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Component
public class MapUtils {
    @Autowired
    private MapProperties mapProperties;

    public LngLatInfoVo geocodingUrl(String addr) {
        try {
            addr = addr.trim();
            String addressUrl = String.format(mapProperties.getGeocodingUrl(), addr);
            String res = HttpRequestUtils.get(addressUrl);
            log.info("res:{}", res);
            JSONObject obj = JSON.parseObject(res);
            if (obj.getInteger("status") == 0) {
                return obj.getObject("result", LngLatInfoVo.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new LngLatInfoVo();
    }
}
